package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.error.HttpsApplyException;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.certificate.entity.SslCertificateVO;
import cn.xenosp.server.module.certificate.service.SslCertificateService;
import cn.xenosp.server.module.emailAccount.entity.SysMailAccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-09-04 11:14
 * 描述: sse
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/ssl")
public class SslController {

    @Autowired
    private SslCertificateService sslCertificateService;

    @LogInfo(operationModuleType = "证书管理模块",operationName = "查询证书",operationContent = "查询证书")
    @PostMapping("/getPage")
    public Result<?> getPage(@RequestBody String body){
        try {
            SslCertificateVO sslCertificateVO = ObjectConvert.urlStringToClass(body, SslCertificateVO.class);
            return Result.OK("",sslCertificateService.getPage(sslCertificateVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "证书管理模块",operationName = "新增证书",operationContent = "新增证书")
    @PostMapping("/add")
    public Result<?> add(@RequestBody SslCertificateVO  certificateVO){
        try {
            sslCertificateService.add(certificateVO);
            return Result.OK();
        }catch (HttpsApplyException e){
            return Result.error(e.getMessage(),null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "证书管理模块",operationName = "修改证书",operationContent = "修改证书")
    @PostMapping("/save")
    public Result<?> save(@RequestBody SslCertificateVO certificateVO){
        try {
            sslCertificateService.save(certificateVO);
            return Result.OK("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "证书管理模块",operationName = "删除证书",operationContent = "删除证书")
    @PostMapping("/del")
    public Result<?> del(@RequestBody SslCertificateVO  certificateVO){
        try {
            if (certificateVO.getId().contains(",")){
                sslCertificateService.delete(certificateVO.getId().split(","));
            }else {
                sslCertificateService.delete(certificateVO.getId());
            }
            return Result.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "证书管理模块",operationName = "申请证书",operationContent = "申请证书")
    @PostMapping("/apply")
    public Result<?> apply(@RequestBody List<SslCertificateVO> certificateList){
        try {
            sslCertificateService.applyCertificate(certificateList);
            return Result.OK("申请中，请稍后刷新列表");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "证书管理模块",operationName = "续签证书",operationContent = "续签证书")
    @PostMapping("/renew")
    public Result<?> renew(@RequestBody List<SslCertificateVO> certificateList){
        try {
            sslCertificateService.renewCertificate(certificateList);
            return Result.OK("续签中，请稍后刷新列表");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
}
